<template>
  <div>
    <!-- 查询区域 -->
    <div class="table-page-search-wrapper">
      <a-form layout="inline" @keyup.enter.native="searchQuery">
        <a-row :gutter="24">
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.system.sysfillrule.entity.ruleName')">
              <a-input placeholder="" v-model="queryParam.ruleName"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <a-form-item :label="$t('m.system.sysfillrule.entity.ruleCode')">
              <a-input placeholder="" v-model="queryParam.ruleCode"></a-input>
            </a-form-item>
          </a-col>
          <a-col :md="6" :sm="8">
            <span style="float: left;overflow: hidden;" class="table-page-search-submitButtons">
              <a-button type="primary" @click="searchQuery" icon="search">{{ $t('m.common.button.query') }}</a-button>
              <a-button type="default" @click="searchReset" icon="reload" style="margin-left: 8px">{{ $t('m.common.button.reset') }}</a-button>
            </span>
          </a-col>
        </a-row>
      </a-form>
    </div>

    <!-- 操作按钮区域 -->
    <div class="table-operator">
      <a-button @click="handleAdd" type="primary" icon="plus">{{ $t('m.common.button.add') }}</a-button>
      <a-button @click="batchDel" type="default"  icon="delete">{{ $t('m.common.button.delete') }}</a-button>
      <a-button type="default" icon="download" @click="handleExportXls($t('m.system.sysfillrule.other.sysFillRule_tab'))">{{ $t('m.common.button.export') }}</a-button>
      <a-upload name="file" :showUploadList="false" :multiple="false" :headers="tokenHeader" :action="importExcelUrl"
                @change="handleImportExcel">
        <a-button type="default" icon="import">{{ $t('m.common.button.import') }}</a-button>
      </a-upload>
    </div>

    <!-- table区域-begin -->

    <a-table
      ref="table"
      size="middle"
      bordered
      rowKey="id"
      :columns="columns"
      :dataSource="dataSource"
      :pagination="ipagination"
      :loading="loading"
      :rowSelection="{selectedRowKeys: selectedRowKeys, onChange: onSelectChange}"
      @change="handleTableChange">
        <template slot="serialNum" slot-scope="text,record,index">
          {{(ipagination.current-1) * ipagination.pageSize + parseInt(index) + 1}}
        </template>
        <span slot="action" slot-scope="text, record">
          <a @click="handleEdit(record)">{{ $t('m.common.button.update') }}</a>
          <a-divider type="vertical"/>
          <a @click="handleTest(record)">{{ $t('m.common.button.test') }}</a>
          <a-divider type="vertical"/>
          <a-popconfirm :title="$t('m.common.tip.delete_confirm')" @confirm="() => handleDelete(record.id)">
            <a>{{ $t('m.common.button.delete') }}</a>
          </a-popconfirm>
        </span>

    </a-table>
    <!-- table区域-end -->

    <!-- 表单区域 -->
    <sys-fill-rule-modal ref="modalForm" @ok="modalFormOk"/>
  </div>
</template>

<script>

  import { getAction } from '@/api/manage'
  import SysFillRuleModal from './modules/SysFillRuleModal'
  import { YcListMixin } from '@/mixins/YcListMixin'
  import YchConstant from "@/api/YchConstant";

  export default {
    name: 'SysFillRuleList',
    mixins: [YcListMixin],
    components: { SysFillRuleModal },
    data () {
      return {
        description: '编码生成规则页面',
        // 表头
        columns: [
          {
            title:this.$t('m.common.label.number'),
            dataIndex: '',
            key: 'rowIndex',
            width: 60,
            align: 'center',
            scopedSlots: { customRender: 'serialNum' },
          },
          {
            title: this.$t('m.system.sysfillrule.entity.ruleName'),
            align: 'center',
            dataIndex: 'ruleName'
          },
          {
            title: this.$t('m.system.sysfillrule.entity.ruleCode'),
            align: 'center',
            dataIndex: 'ruleCode'
          },
          {
            title: this.$t('m.system.sysfillrule.entity.ruleClass'),
            align: 'center',
            dataIndex: 'ruleClass'
          },
          /*{
            title: '参数',
            align: 'center',
            dataIndex: 'ruleParams'
          },*/
          {
            title: this.$t('m.common.label.operate'),
            dataIndex: 'action',
            align: 'center',
            scopedSlots: { customRender: 'action' },
            width:240
          }
        ],
        url: {
          list: YchConstant.PREFIX_SYSTEM + '/fillRule/list',
          test: YchConstant.PREFIX_SYSTEM + '/fillRule/testFillRule',
          delete: YchConstant.PREFIX_SYSTEM + '/fillRule/delete',
          deleteBatch: YchConstant.PREFIX_SYSTEM + '/fillRule/deleteBatch',
          exportXlsUrl: YchConstant.PREFIX_SYSTEM + '/fillRule/exportExcel',
          importExcelUrl: YchConstant.PREFIX_SYSTEM + '/fillRule/importExcel',
        },
      }
    },
    computed: {
      importExcelUrl () {
        return `${bootConfig[process.env.NODE_ENV].VUE_APP_API}${this.url.importExcelUrl}`
      }
    },
    methods: {
      handleTest (record) {
        let closeLoading = this.$message.loading('生成中...', 0)

        getAction(this.url.test, {
          ruleCode: record.ruleCode
        }).then(res => {
          if (res.success) {
            this.messageUtil.info(this.$t('m.system.sysfillrule.other.generated_code') + res.result);
          } else {
            this.messageUtil.warning(res.message)
          }
        }).finally(() => {
          closeLoading()
        })
      }
    }
  }
</script>
<style lang="less" scoped>

</style>
